/*
 * Author:   W.SH
 */
package com.wgg.crawler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.wgg.crawler.core.SpiderResponse;
import com.wgg.crawler.parser.Fetcher;
import com.wgg.crawler.parser.Handler;
import com.wgg.crawler.scheduler.BloomDepthScheduler;
import com.wgg.crawler.scheduler.BloomScheduler;
import com.wgg.crawler.scheduler.Scheduler;
import com.wgg.crawler.util.LinksList;
import com.wgg.crawler.util.RegexRule;



/**
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉
 *
 *  @author W.SH
 */
public class Spider {
    
    public static void main(String[] args) throws IOException {
        zongHengSpider();
    }
    
    /**
     *  抓取百度百科的标题
     */
    private static  void baiduBaikeSpider() {
        Scheduler scheduler = new BloomScheduler();
        scheduler.offer(Arrays.asList("http://baike.baidu.com/"));
        new Fetcher(scheduler, new Handler() {
            @Override
            public void onSuccess(SpiderResponse response) {
                try {
                    Element element = Jsoup.parse(new String(response.getContent()));
                    Element titleElement = element.getElementsByClass("lemmaWgt-lemmaTitle-title").first();
                    Element contentElement = element.getElementsByClass("lemma-summary").first();
                    if (null == titleElement || null == contentElement ||!Pattern.matches(".*亨利.*", contentElement.getElementsByClass("para").text())) {
                        return;
                    }
                    String result = "\t" + titleElement.getElementsByTag("h1").text() + "\n \t" + contentElement.getElementsByClass("para").text() + "\n------------------------------------------------------------\n\n"; 
                    System.out.println(result);
                    File resultFile  = new File("D:\\reulst.txt");
                    if (!resultFile.exists()) {
                        resultFile.createNewFile();
                    }
                    RandomAccessFile raf = new RandomAccessFile(resultFile,"rw");
                    long nowPosition = raf.length();
                    FileChannel outChannel = raf.getChannel();
                    outChannel.write(ByteBuffer.wrap(result.getBytes()), nowPosition);
                    outChannel.force(true);
                    outChannel.close();
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            @Override
            public void onFail(SpiderResponse response) {
                System.out.println("提取成功 url为 \n" + response.getUrl());
            }
            @Override
            public List<String> handleAndGetLinks(SpiderResponse response) {
                LinksList list = new LinksList();
                RegexRule regexRule = new RegexRule();
                regexRule.addPositive(Collections.singletonList("http://baike.baidu.com/\\S+"));
                regexRule.addNegative("http://baike.baidu.com/redirect/\\S+");
                list.getLinkByA(Jsoup.parse(new String(response.getContent())), regexRule);
                return list;
            }
        }).doFetche();
    }
    
    private static void zongHengSpider() {
        Scheduler scheduler = new BloomDepthScheduler();
        scheduler.offer(Arrays.asList("http://www.zongheng.com/"));
        new Fetcher(scheduler, new Handler() {
            @Override
            public void onSuccess(SpiderResponse response) {
                try {
                    Document document = Jsoup.parse(new String(response.getContent()));
                    Elements elements = document.select(".mind_showbox").select(".tabC").attr("data-zlog", "106").last().select(".cover"); 
                    if (null == elements) {
                        return;
                    }
                    StringBuilder sb = new StringBuilder();
                    for (Element e : elements) {
                        sb.append("\t" + e.attr("title") +"\n\t" + e.absUrl("href") + "\n") ; 
                    }
                    File resultFile  = new File("D:\\reulst.txt");
                    if (!resultFile.exists()) {
                        resultFile.createNewFile();
                    }
                    RandomAccessFile raf = new RandomAccessFile(resultFile,"rw");
                    long nowPosition = raf.length();
                    FileChannel outChannel = raf.getChannel();
                    outChannel.write(ByteBuffer.wrap(sb.toString().getBytes()), nowPosition);
                    outChannel.force(true);
                    outChannel.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFail(SpiderResponse response) {
                System.out.println("提取成功 url为 \n" + response.getUrl());
            }
            @Override
            public List<String> handleAndGetLinks(SpiderResponse response) {
                LinksList list = new LinksList();
                RegexRule regexRule = new RegexRule();
                regexRule.addPositive(Collections.singletonList("http://www.zongheng.com/\\S+"));
                list.getLinkByA(Jsoup.parse(new String(response.getContent())), regexRule);
                return list;
            }
        }, 2).doFetche();
    }
}
